from encodings.utf_8 import encode

import scrapy
import csv

pages = 25
id = 290008


class CcnbSpider(scrapy.Spider):
    name = 'ccnb'

    def start_requests(self):
        urls = ['https://ccnb.syqianyun.xyz/api/groupon/getCardList?page={}'.format(i) for i in range(1, pages + 1)]

        body = {'groupon_id': id, 'name': ''}
        for url in urls:
            yield scrapy.http.JsonRequest(url=url, data=body, callback=self.parse)

    def parse(self, response, **kwargs):
        kami_list = response.json()['data']['data']

        rows = []
        for kami in kami_list:
            setName = kami.get('card_item', '')
            num = kami.get('number', '')
            playerName = kami.get('player_en', '')
            team = kami.get('column1_English', '')
            seq = kami.get('limit_edition', '')
            playerName_zn = kami.get('player_zh', '')
            team_zn = kami.get('column2', '')
            # teamSimpleName = kami['teamSimpleName']

            rows.append((setName, num, playerName, team, seq, playerName_zn, team_zn, '是', '是', '是'))

        with open('ccnb_{}.csv'.format(id), 'a+', newline='', encoding='gb18030') as f:
            writer = csv.writer(f)
            writer.writerows(rows)
